from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys

# 设置 ChromeDriver 路径
# chrome_driver_path = 'f:\\tmp\\chromedriver.exe'
chrome_driver_path = r"C:\Windows\System32\chromedriver.exe"

# 设置 Chrome 浏览器选项
chrome_options = Options()
chrome_options.add_argument("--start-maximized")  # 启动时最大化窗口

# 创建 ChromeDriver 服务
service = Service(chrome_driver_path)

# 初始化 WebDriver
driver = webdriver.Chrome(service=service, options=chrome_options)

try:
    # 打开 Google 首页
    driver.get('https://www.google.com')

    # 查找搜索框
    search_box = driver.find_element(By.NAME, 'q')

    # 在搜索框中输入查询词
    search_box.send_keys('Selenium Python')

    # 提交搜索
    search_box.send_keys(Keys.RETURN)

    # 等待一段时间查看搜索结果
    driver.implicitly_wait(10)  # 等待 10 秒

finally:
    # 关闭浏览器
    driver.quit()
